<template>
  <div class="app-container">
    <el-card v-show="showSearch" class="mb10" shadow="hover">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="120px" size="small">
        <el-form-item label="会员ID" prop="params.memberId">
          <el-input
              v-model="queryParams.params.memberId"
              clearable
              placeholder="请输入会员ID"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="优惠券名称" prop="params.couponName">
          <el-input
              v-model="queryParams.params.couponName"
              clearable
              placeholder="请输入优惠券名称"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="优惠券类型" prop="params.couponType">
          <el-select v-model="queryParams.params.couponType" clearable placeholder="请输入优惠券类型">
            <el-option
                v-for="dict in dict.type.coupon_type"
                :key="dict.dictCode"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="ml10">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              v-hasPermi="['market:couponUsed:export']"
              icon="el-icon-download"
              plain
              size="mini"
              type="warning"
              @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :columns="columns" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="couponUsedList" border @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column v-if="columns[0].visible" align="center" label="优惠券ID" prop="couponId"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[1].visible" align="center" label="券模板ID" prop="templateId"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[2].visible" align="center" label="券名称" prop="couponName"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[3].visible" align="center" label="优惠券类型" prop="couponType"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[4].visible" align="center" label="商户ID" prop="mchId" show-overflow-tooltip/>
        <el-table-column v-if="columns[5].visible" align="center" label="会员ID" prop="memberId" show-overflow-tooltip/>
        <el-table-column v-if="columns[6].visible" align="center" label="开始时间" prop="startTime"
                         show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[7].visible" align="center" label="结束时间" prop="endTime" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[8].visible" align="center" label="发送时间" prop="sendTime"
                         show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.sendTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[9].visible" align="center" label="发券业务编码" prop="sendCode"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[10].visible" align="center" label="使用时间" prop="useTime"
                         show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.useTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[11].visible" align="center" label="销券业务编码" prop="useCode"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[12].visible" align="center" label="使用油站" prop="useStationId"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[13].visible" align="center" label="优惠金额" prop="discountAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[14].visible" align="center" label="商品名称" prop="commodityName"
                         show-overflow-tooltip/>
        <el-table-column v-if="columns[15].visible" align="center" label="创建时间" prop="createDate"
                         show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[16].visible" align="center" label="备注信息" prop="remark"
                         show-overflow-tooltip/>
      </el-table>

      <pagination
          v-show="total>0"
          :limit.sync="queryParams.pageSize"
          :page.sync="queryParams.pageNum"
          :total="total"
          @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {listCouponUsed} from "@/api/market/couponUsed";
import {formatDateTime, nowCnFormatDateTime} from "@/utils/date";

export default {
  name: "CouponUsed",
  dicts: ['coupon_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券使用记录表格数据
      couponUsedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortName: 'id',
        sortOrder: 'desc',
        params: {
          couponId: undefined,
          couponType: undefined,
          templateId: undefined,
          couponName: undefined,
          memberId: undefined,
          startTime: undefined,
          endTime: undefined,
          sendTime: undefined,
          sendCode: undefined,
          useTime: undefined,
          useCode: undefined,
          useStationId: undefined,
          discountAmount: undefined,
          commodityName: undefined
        }
      },
      // 列信息
      columns: [
        {key: 0, label: `优惠券ID`, visible: false},
        {key: 1, label: `券模板ID`, visible: false},
        {key: 2, label: `券名称`, visible: true},
        {key: 3, label: `优惠券类型`, visible: true},
        {key: 4, label: `商户ID`, visible: false},
        {key: 5, label: `会员ID`, visible: true},
        {key: 6, label: `开始时间`, visible: true},
        {key: 7, label: `结束时间`, visible: true},
        {key: 8, label: `发送时间`, visible: false},
        {key: 9, label: `发券业务编码`, visible: false},
        {key: 10, label: `使用时间`, visible: false},
        {key: 11, label: `销券业务编码`, visible: true},
        {key: 12, label: `使用油站`, visible: true},
        {key: 13, label: `优惠金额`, visible: true},
        {key: 14, label: `商品名称`, visible: true},
        {key: 15, label: `创建时间`, visible: false},
        {key: 16, label: `备注信息`, visible: true},
      ],
      // 表单参数
      form: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDateTime,
    /** 查询优惠券使用记录列表 */
    getList() {
      this.loading = true;
      listCouponUsed(this.queryParams).then(response => {
        this.couponUsedList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleSortChange(data) {
      console.log('handleSortChange')
      console.log(data)
      if (data.order) {
        if (data.order.indexOf('asc') > -1) {
          this.queryParams.sortOrder = 'asc'
        } else {
          this.queryParams.sortOrder = 'desc'
        }
        this.queryParams.sortName = data.prop
      }
      console.log(this.queryParams)
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/couponUsed/export', {
        ...this.queryParams
      }, `couponUsed_${nowCnFormatDateTime()}.xlsx`)
    }
  }
}

</script>

<style>
.el-form .el-select {
  width: 100%;
}
</style>
